﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CrashSimul8or
{
    class FireParticleEngine : ParticleEngine
    {
        private float flameSize;
        public float flameHealth;

        /// <summary>
        /// Smiður sem tekur sömu breytur og ParticleEngine en einnig hversu stór eldurinn
        /// á að vera.
        /// </summary>
        /// <param name="textures">Listi af textures fyrir eldinn</param>
        /// <param name="location">Staðsetning eldsins</param>
        /// <param name="newParticlesPerUpdate">Fjöldi nýrra particles í hverri umferð</param>
        /// <param name="flameSize">Stærð eldsins</param>
        public FireParticleEngine(List<Texture2D> textures, Vector2 location, int newParticlesPerUpdate, float flameSize)
            : base(textures, location, newParticlesPerUpdate)
        {
            this.flameHealth = 1f;
            this.flameSize = flameSize;
        }

        /// <summary>
        /// Gerir það sama og Update aðferðin í ParticleEngine auk þess að breyta lit á particles.
        /// </summary>
        public override void Update()
        {
            foreach (Particle p in particles)
            {
                getIntensity(p);
            }
            base.Update();
        }

        /// <summary>
        /// Aðferð sem býr til particles fyrir eldinn, með tilheyrandi eiginleikum.
        /// </summary>
        /// <returns>Nýrri eld Particle</returns>
        protected override Particle GenerateNewParticle()
        {
            return new Particle(textures[random.Next(textures.Count)],
                                        EmitterLocation,
                                        new Vector2(
                                        flameSize * (flameHealth) * ((float)(random.NextDouble()) - 0.5f),
                                        -flameSize * (flameHealth) * (float)(random.NextDouble() * 2)),
                                        0,
                                        0,
                                        new Color(255, 255, 255),
                                        0.25f,
                                        20 + random.Next(40));
        }

        /// <summary>
        /// Finnur réttann lit á Particles með eftirfarandi jöfnu.
        /// fire  R   G   B
        ///   0   0   0   0
        ///   1   3   0   0
        ///   2   6   0   0
        ///   .   .   .   .
        ///  84 252   0   0
        ///  85 255   0   0
        ///  86 255   3   0
        ///   .   .   .   .
        /// 169 255 252   0
        /// 170 255 255   0
        /// 171 255 255   3
        ///   .   .   .   .
        /// 254 255 255 252
        /// 255 255 255 255
        /// </summary>
        /// <param name="p">Viðeigandi particle Particle</param>
        private void getIntensity(Particle p)
        {
            float intensity = ((float)p.TTL / (float)p.LifeTime);
            Vector4 newColor = new Vector4();
            if (GameVariables.fadingParticles)
            {
                newColor.W = intensity;
            }
            else
            {
                newColor.W = 1.0f;
            }
            newColor.X = Math.Min(intensity, 85.0f / 255.0f) * 3.0f;
            intensity = Math.Max(intensity - 85.0f / 255.0f, 0);
            newColor.Y = Math.Min(intensity, 85.0f / 255.0f) * 3.0f;
            intensity = Math.Max(intensity - 85.0f / 255.0f, 0);
            newColor.Z = Math.Min(intensity, 85.0f / 255.0f) * 3.0f;

            p.Color = new Color(newColor);
        }
    }
}
